package com.fingard.dsp.bank.directbank.cgb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 广发银行单笔实时代扣
 * 
 * @author Cruise
 * @date 2019/4/26
 */
public class CGB02RealTime extends CGB02Base implements IRealTime {
	@Override
	public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
		
		try {
			CGB02Header bankHeader = new CGB02Header();
			bankHeader.reqSeqID = p_req9108.singleDetail.askNumber;
			bankHeader.transCode = "2040";
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, bankHeader);
			
			xmlWriter.writeStartElement("Body");
			xmlWriter.writeElementString("signBankNo", p_req9108.singleDetail.authCode);
			xmlWriter.writeElementString("outAcc", p_req9108.singleDetail.payAct);
			xmlWriter.writeElementString("outAccName", p_req9108.singleDetail.payName);
			xmlWriter.writeElementString("intAcc", p_req9108.singleDetail.recAct);
			xmlWriter.writeElementString("amount", p_req9108.singleDetail.payAmount);
			xmlWriter.writeElementString("remark", "");
			xmlWriter.writeElementString("comment", "");
			xmlWriter.writeElementString("reserve1", p_req9108.singleDetail.askNumber);
			xmlWriter.writeElementString("reserve2", "");
			xmlWriter.writeElementString("reserve3", "");
			xmlWriter.writeElementString("reserve4", "");
			xmlWriter.writeElementString("reserve5", "");
			xmlWriter.writeEndDocument();
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			if(tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					String tmpRespCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
					if ("000".equals(tmpRespCode)) {
						tmpRetDetail.transState = "D" + tmpBkReader.getFirstText("/BEDC/Message/Body/status");
						tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BEDC/Message/Body/errMsg");
						String transInfo = tmpBkReader.getFirstText("/BEDC/Message/Body/errMsg");
						if (StringHelper.hasAnyChar(transInfo)) {
							tmpRetDetail.respInfo = transInfo;
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpRespCode;
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1"; 
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9144.askNumber;
		
		try {
			CGB02Header bankHeader = new CGB02Header();
			bankHeader.reqSeqID = p_req9144.askNumber;
			bankHeader.transCode = "2041";
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, bankHeader);
			
			xmlWriter.writeStartElement("Body");
			xmlWriter.writeElementString("customerSalarySeq", p_req9144.askNumber);
			xmlWriter.writeElementString("tranDate", Format.Date8Format.format(p_req9144.getBgnDateTime()));
			xmlWriter.writeElementString("reserve1", "");
			xmlWriter.writeEndDocument();
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			if(tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					String tmpRespCode = tmpBkReader.getFirstText("/BEDC/Message/commHead/retCode");
					if ("000".equals(tmpRespCode)) {
						tmpRetDetail.transState = tmpRespCode + "+" + tmpBkReader.getFirstText("/BEDC/Message/Body/status");
						tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/BEDC/Message/Body/errMsg");
						String transInfo = tmpBkReader.getFirstText("/BEDC/Message/Body/errMsg");
						if (StringHelper.hasAnyChar(transInfo)) {
							tmpRetDetail.respInfo = transInfo;
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpRespCode;
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1"; 
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
